﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseEntities;
using BusinessLogic;
namespace Mock1Group6
{
    public partial class Welcome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["click"] != null)
            {
                Session["Block"] = null;
                if (Session["insertcard"] == null)
                {
                    txtInsertCard.Focus();
                }
                else
                {
                    txtPIN.Focus();
                }
            }
            else
            {
                Label4.Visible = false;
                Label3.Visible = false;
                Label2.Visible = false;
                Label1.Visible = true;
                btnOK.Visible = false;
                txtInsertCard.Focus();
                txtPIN.Visible = false;
                btnCancel2.Visible = false;
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Card c = new Card();
            c.PIN = txtPIN.Text;

            if (Session["insertcard"] != null)
            {
                string CardNo = Session["CardNo"].ToString();
                c.CardNo = CardNo;

                if (CardBus.ValidatePIN(c) == false)
                {
                    if (Session["Block"] == null)
                    {
                        Label1.Visible = true;
                        Session["Dem"] = (int)Session["Dem"] + 1;
                        Label2.Visible = true;
                        Label2.Text = "Enter your PIN";
                        Label1.Visible = false;
                        Label4.Visible = true;
                        Label4.Text = "You entered a wrong PIN";
                        btnCancel2.Visible = true;
                        Label3.Visible = true;
                        Label3.Text = "Then press enter button";
                        txtPIN.Text = "";
                        txtPIN.Focus();
                    }
                    else
                    {
                        Response.Redirect("Welcome.aspx");
                        Session["insertcard"] = null;
                        Session["click"] = null;
                        Session["CardNo"] = null;
                        Session["Block"] = null;
                        Session["Dem"] = 0;
                        txtPIN.Text = "";
                    }
                }

                else
                {
                    Response.Redirect("FormMain.aspx");
                    txtPIN.Text = "";
                }
            }
            else
            {
                Label2.Visible = false;
                btnOK.Visible = false;
                Label1.Visible = true;
                Session["CardNo"] = null;
                txtInsertCard.Text = "";
            }

            if ((int)Session["Dem"] > 3)
            {
                CardBus.UpdateCard(c);
                Label4.Visible = false;
                Label3.Visible = false;
                Label2.Visible = true;
                Label2.Text = "Card is Block";
                btnOK.Visible = true;
                txtInsertCard.Focus();
                txtPIN.Visible = false;
                btnCancel2.Visible = false;
                Session["Block"] = "Block";
                Session["insertcard"] = null;
                Session["Dem"] = 0;
                txtPIN.Text = "";
            }
        }
        protected void btn01_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "1";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "1";
        }

        protected void btn02_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "2";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "2";
        }

        protected void btn03_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "3";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "3";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                Label4.Visible = false;
                Label3.Visible = false;
                Label2.Visible = false;
                Label1.Visible = true;
                btnOK.Visible = false;
                txtInsertCard.Focus();
                txtPIN.Visible = false;
                btnCancel2.Visible = false;
                txtPIN.Text = "";
                Session["insertcard"] = null;
            }
        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "4";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "4";
        }

        protected void btn05_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "5";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "5";
        }

        protected void btn06_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "6";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "6";
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = "";
            }
            else
                txtInsertCard.Text = "";
        }

        protected void btn08_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "8";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "8";
        }

        protected void btn9_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "9";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "9";
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            Card c = new Card();
            c.PIN = txtPIN.Text;

            if (Session["insertcard"] != null)
            {
                string CardNo = Session["CardNo"].ToString();
                c.CardNo = CardNo;

                if (CardBus.ValidatePIN(c) == false)
                {
                    if (Session["Block"] == null)
                    {
                        Label1.Visible = true;
                        Session["Dem"] = (int)Session["Dem"] + 1;
                        Label2.Visible = true;
                        Label2.Text = "Enter your PIN";
                        Label1.Visible = false;
                        Label4.Visible = true;
                        Label4.Text = "You entered a wrong PIN";
                        btnCancel2.Visible = true;
                        Label3.Visible = true;
                        Label3.Text = "Then press enter button";
                        txtPIN.Text = "";
                        txtPIN.Focus();
                    }
                    else
                    {
                        Response.Redirect("Welcome.aspx");
                        Session["insertcard"] = null;
                        Session["click"] = null;
                        Session["CardNo"] = null;
                        Session["Block"] = null;
                        Session["Dem"] = 0;
                        txtPIN.Text = "";
                    }
                }
                else
                {
                    Response.Redirect("FormMain.aspx");
                    txtPIN.Text = "";
                }
            }
            else
            {
                Label2.Visible = false;
                btnOK.Visible = false;
                Label1.Visible = true;
                Session["CardNo"] = null;
                txtInsertCard.Text = "";
            }

            if ((int)Session["Dem"] > 3)
            {
                CardBus.UpdateCard(c);
                Label4.Visible = false;
                Label3.Visible = false;
                Label2.Visible = true;
                Label2.Text = "Card is Block";
                btnOK.Visible = true;
                txtInsertCard.Focus();
                txtPIN.Visible = false;
                btnCancel2.Visible = false;
                Session["Block"] = "Block";
                Session["insertcard"] = null;
                Session["Dem"] = 0;
                txtPIN.Text = "";
            }
        }

        protected void btnCham_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + ".";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + ".";
        }

        protected void btn0_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "0";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "0";
        }

        protected void btnRong_Click(object sender, EventArgs e)
        {

        }

        protected void btn07_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                txtPIN.Text = txtPIN.Text + "7";
            }
            else
                txtInsertCard.Text = txtInsertCard.Text + "7";
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Session["click"] = "click";
            Card c = new Card();
            c.CardNo = txtInsertCard.Text;
            if (CardBus.ValidateCard(c) == true)
            {
                Session["insertcard"] = "insertcard";
                Session.Add("CardNo", txtInsertCard.Text);
                txtPIN.Focus();
                Label2.Visible = true;
                Label2.Text = "Enter your PIN";
                txtPIN.Visible = true;
                Label4.Visible = false;
                btnOK.Visible = true;
                Label1.Visible = false;
                btnCancel2.Visible = true;
                Label3.Visible = true;
                Label3.Text = "Then press enter button";
            }
            else
            {
                Label1.Visible = false;
                Label2.Visible = true;
                Label2.Text = "System does not recognize ATM card eject card...";
                btnOK.Visible = true;
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (Session["insertcard"] != null)
            {
                Label4.Visible = false;
                Label3.Visible = false;
                Label2.Visible = false;
                Label1.Visible = true;
                btnOK.Visible = false;
                txtInsertCard.Focus();
                txtPIN.Visible = false;
                btnCancel2.Visible = false;
                txtPIN.Text = "";
                Session["insertcard"] = null;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }
    }
}